#include "AManOfMapsState.h"

AManOfMapsState AManOfMapsState::m_AManOfMapsState;

void AManOfMapsState::Init(sf::RenderWindow& window){

    //++ Initialization +++
    playerTile = 25500;


    // +++ TGUI Stuff +++
    gui.setWindow(window);
    gui.setGlobalFont("resources/arial.ttf");

    //Return to menu button
    tgui::Button::Ptr button2(gui);
    button2->load("resources/Black.conf");
    button2->setSize(150, 25);
    button2->setPosition(25, 50);
    button2->setText("Look Closer");
    button2->bindCallback(tgui::Button::LeftMouseClicked);
    button2->setCallbackId(1);
    button2->setTextColor(sf::Color::White);



    // +++ Tiles +++
    int width = 200;
    int height = 200;
    int startX = 25;
    int startY = 20;
    int block_width = 64;
    int block_height = 64;
    for(int i = 0; i < height; i++ ){
        for(int j = 0; j < width; j++ ){
                tiles.push_back(new tile(startX +j * block_width, startY + i* block_height, block_width, block_height));
        }
    }
    std::cout << "Number of Tiles: " << tiles.size() << std::endl ;


    //Border Image

    if(!mapBorderTexture.loadFromFile("resources/map4.png")){
        std::cout << "Map Border Texture did not load";
    }
    mapBorderTexture.setSmooth(true);
    mapBorder.setTexture(mapBorderTexture);





    //create the Tilemap
    if (!map.load("resources/man_of_maps.png", sf::Vector2u(block_width, block_height), tiles, width, height)){

    }

    map.setPosition(sf::Vector2f(startX,startY));

        //Player

    if(!pTexture.loadFromFile("resources/mom_player.png")){
        std::cout << "Player Texture did not load";
    }
    playerPosition = tiles[playerTile]->rect.getPosition();
    player.setTexture(pTexture);
    player.setTextureRect(sf::IntRect(0,0,64,64));
    player.setPosition(playerPosition);

    // +++ VIEWS +++
    view.setCenter(playerPosition.x + 32, playerPosition.y + 32);
    view.setSize(window.getSize().x, window.getSize().y);
    view.zoom(.2);
    window.setView(view);
    //button view
    view2.setCenter(window.getSize().x / 2, window.getSize().y / 2);
    view2.setSize(window.getSize().x, window.getSize().y);



}


void AManOfMapsState::Cleanup(){}

void AManOfMapsState::Pause(){}
void AManOfMapsState::Resume(){}

void AManOfMapsState::Events(GameEngine* game, sf::RenderWindow& window){


    window.setView(view2);

    while (window.pollEvent(event)){
        // Close the window and end the game when X clicked
        if (event.type == sf::Event::Closed){
            game->Quit();
        }

        //handle tGUI events
        gui.handleEvent(event);

        // The callback loop
        tgui::Callback callback;



        while (gui.pollCallback(callback)){
                // Change the tile where the player is standing
                if (callback.id == 1){
                    std::cout << "Button 1 Pressed" << std::endl;
                    tiles[playerTile]->type += 1;
                    map.resetTileMap();
                    std::cout << "Tile " << playerTile << " set to " << tiles[playerTile]->type << std::endl;
                    view.zoom(0.90f);
                }
        }

        /*
        if (event.type == sf::Event::MouseButtonPressed){

            //LEFT CLICK
            if(event.mouseButton.button == sf::Mouse::Left){

                sf::Vector2i windowPos = sf::Mouse::getPosition(window);
                sf::Vector2f mouseWorldPos = window.mapPixelToCoords(windowPos);

                // check all Entities - Toggle on if clicked by the mouse
                for(int i = 0;i < tiles.size();i++) {
                    if(tiles[i]->rect.getGlobalBounds().contains(mouseWorldPos)){
                        tiles[i]->type += 1;
                        map.resetTileMap();
                        std::cout << "Inside Tile # " << i << " of " << tiles.size() << std::endl;
                        break;
                    }
                }
            }
        }
        */

        //Zoom view in and out using the mouse scrollwheel
        if (event.type == sf::Event::MouseWheelMoved){
            if(event.mouseWheel.delta >= 1){
                view.zoom(0.95f);
            }else if (event.mouseWheel.delta <= -1){
                view.zoom(1.05f);
            }
            std::cout  << "Window Size: " << view.getSize().x << " " << view.getSize().y << std::endl;
        }

    }

}

void AManOfMapsState::Update(GameEngine* game, sf::RenderWindow& window){

}

void AManOfMapsState::Render(GameEngine* game, sf::RenderWindow& window){

    window.setView(view);

    window.clear(sf::Color::White);



    window.draw(map);
    window.draw(player);

    //window border
    //window.draw(mapBorder);

    for(int i = 0; i < tiles.size(); i++){
        //window.draw(tiles[i]->rect);
    }

    window.setView(view2);
    gui.draw();

    window.display();

}
